﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

using System;
using System.IO;
using System.Text;


public static class CsvUtility
{
    // Methods
    public static bool ParseCSV(string FileName, out List<Dictionary<string, string>> Values)
    {
        Values = new List<Dictionary<string, string>>();
        TextAsset assetToUnload = Resources.Load(FileName) as TextAsset;
        if (assetToUnload != null)
        {
            CsvFileReader reader = new CsvFileReader(new MemoryStream(Encoding.UTF8.GetBytes(assetToUnload.text)));
            CsvRow row = new CsvRow();
            reader.ReadRow(row);
            reader.ReadRow(row);
            string[] strArray = ToStringArray(row.LineText);
            int length = strArray.Length;
            while (reader.ReadRow(row))
            {
                if (!row.LineText.StartsWith("#"))
                {
                    string[] strArray2 = ToStringArray(row.LineText);
                    Dictionary<string, string> item = new Dictionary<string, string>(length);
                    for (int i = 0; i < length; i++)
                    {
                        item.Add(strArray[i], strArray2[i]);
                    }
                    Values.Add(item);
                }
            }
            Resources.UnloadAsset(assetToUnload);
            return true;
        }
        return false;
    }

    public static byte[] ToByteArray(string Content)
    {
        return Array.ConvertAll<string, byte>(Content.Split(new char[] { ';' }), Value => Convert.ToByte(Value));
    }

    public static double[] ToDoubleArray(string Content)
    {
        return Array.ConvertAll<string, double>(Content.Split(new char[] { ';' }), Value => Convert.ToDouble(Value));
    }

    public static float[] ToFloatArray(string Content)
    {
        return Array.ConvertAll<string, float>(Content.Split(new char[] { ';' }), Value => Convert.ToSingle(Value));
    }

    public static int[] ToIntArray(string Content)
    {
        return Array.ConvertAll<string, int>(Content.Split(new char[] { ';' }), Value => Convert.ToInt32(Value));
    }

    public static string[] ToStringArray(string Content)
    {
        return Content.Split(new char[] { ';' });
    }
}


